import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'
import { getItem } from '@/utils/storage'
import { Dialog } from 'vant'

let originalPush = VueRouter.prototype.push
VueRouter.prototype.push = function push(location, onResolve, onReject) {
  if (onResolve || onReject) return originalPush.call(this, location, onResolve, onReject)
  return originalPush.call(this, location).catch(err => err)
}

const Login = () => import('@/views/login')
const Layout = () => import('@/views/layout')
const Home = () => import('@/views/home')
const Qa = () => import('@/views/qa')
const Video = () => import('@/views/video')
const Profile = () => import('@/views/profile')
const Setting = ()=> import('@/views/profile/UserSetting')
const Search = () => import('@/views/search')
const Article = () => import('@/views/article')
const ReadHistory = () => import('@/views/profile/readHistory')
const MyCollection = () => import('@/views/profile/myCollection')
const Notice = () => import('@/views/profile/Notice')
const MyDynamic = () => import('@/views/profile/myDynamic')
const MyFans = () => import('@/views/profile/myFans')
const UserChart = () => import('@/views/chart')
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Layout',
    component: Layout,
    redirect: '/home',
    children:[
      {
        path: 'home',
        name: 'home',
        component: Home,
        meta: { 
          keepAlive: true
         }
      },
      {
        path: 'qa',
        name: 'qa',
        component: Qa,
        meta: { 
          keepAlive:false
         }
      },
      {
        path: 'video',
        name: 'video',
        component: Video,
        meta: { 
          keepAlive: false
         }
      },
      {
        path: 'profile',
        name: 'profile',
        component: Profile,
        meta: { 
          keepAlive: false
         }
      },
        
      
    ]
  },
  {
    path: '/login',
    name: 'Login',
    component: Login,
    meta: { 
      keepAlive: false
     }
  },
  {
    path: '/search',
    name: 'Search',
    component: Search,
    meta: { 
      keepAlive: false
     }
  },
  {
    path: '/article/:articleId',
    name: 'Article',
    component: Article,
    meta: { 
      keepAlive: false
     }
  },
  {
    path: '/setting',
    name: 'setting',
    component:Setting,
    meta: { 
      keepAlive: false
     }
  },
  {
    path: '/readHistory',
    name: 'readHistory',
    component: ReadHistory,
    meta: {
      isLogin: true,
      keepAlive: true
     }
  },
  {
    path: '/myCollection',
    name: 'myCollection',
    component: MyCollection,
    meta: {
      isLogin: true,
      keepAlive: true
     }
  },
  {
    path: '/notice',
    name: 'notice',
    component: Notice,
    meta: {
      isLogin: true,
      keepAlive: true
     }
  },
  {
    path: '/myDynamic',
    name: 'myDynamic',
    component: MyDynamic,
    meta: {
      isLogin: true,
      keepAlive: true
     }
  },
  {
    path: '/myFans',
    name: 'myFans',
    component: MyFans,
    meta: {
      isLogin: true,
      keepAlive: true
     }
  },
  {
    path: '/userChart',
    name: 'userChart',
    component: UserChart,
    meta: {
      isLogin: true,
      keepAlive: false
     }
  }
]


const router = new VueRouter({
  routes
})



router.beforeEach( (to,from,next) => {
  const userInfo = store.state.user.tokenInfo || getItem('user-token')
  if (to.name === "login" || !to.meta.isLogin) {
     return next()
  }
  
  if (userInfo) {
     return next()
  }
    Dialog.confirm({
      title: '该功能需要登录，确认登录吗？'
    }).then(() => {
      next({
        path: '/login',
      })
    }).catch(() => {

    })
})
export default router
